package cc.ufinity.networkdiagnose.widget;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.view.R;

public class TestWidget_Wlan extends GeneralTestWidget {

	//private TextView xinzaobiceshi;
	private TextView xinhaoqinagdu;
	private TextView ftpceshi;
	private TextView httpceshi;
	private TextView peap;
	private TextView ping;
    private Button backBtn;
	private TextView web;
	private TextView manyouceshi;
	private TextView geliceshi;
	private TextView apguanlian;
	private TextView titleView;
	private TextView largerFileFTPDownload;
	private AbsoluteLayout relation;

	public Activity ctx;
	private Button oneKey;
	private WlanManager wlanManager;
	private Handler handler;

	public TestWidget_Wlan(Activity context) {
		super(context);
		inflate(context, R.layout.networktest_wlan, this);
		this.ctx = context;	
		titleView = (TextView)findViewById(R.id.titleId_wlan); 
		titleView.setText("Wlan网络");
		GlobalApp.isPeapTested = false;
		ctx.getIntent().putExtra(TAG_REFER_TASK, GlobalApp.taskWlan);
		
		largerFileFTPDownload = (TextView) findViewById(R.id.lFile_FTPDowln_wlan);
		xinhaoqinagdu = (TextView) findViewById(R.id.levelbutton_wlan);
		ftpceshi = (TextView) findViewById(R.id.ftpbutton_wlan);
		httpceshi = (TextView) findViewById(R.id.httpbutton_wlan);
		peap = (TextView) findViewById(R.id.peapbutton_wlan);
		//sim = (TextView) findViewById(R.id.simbutton_wlan);
		ping = (TextView) findViewById(R.id.pingbutton_wlan);
		web = (TextView) findViewById(R.id.webbutton_wlan);
		manyouceshi = (TextView) findViewById(R.id.manyouceshi_wlan);
		geliceshi = (TextView) findViewById(R.id.geliceshi_wlan);
		apguanlian = (TextView) findViewById(R.id.apguanlian_wlan);
		relation = (AbsoluteLayout) findViewById(R.id.relationjianmian_wlan);
        backBtn = (Button) findViewById(R.id.test_btnback_wlan);
		oneKey = (Button) this.findViewById(R.id.UPLOADButton_wlan);
        backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ctx.setContentView(new SelectNetType_main(ctx));
            }
        });
		oneKey.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					oneKey.setBackgroundResource(R.drawable.yijianceshibeijing);
				} else if (event.getAction() == KeyEvent.ACTION_UP) {
					oneKey.setBackgroundResource(R.drawable.top);
				}
				return false;
			}
		});

		largerFileFTPDownload.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_xzb);
				}
				return false;
			}
		});
		largerFileFTPDownload.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doLfileFTPDownload();
			}
		});
		
		xinhaoqinagdu.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_rssi);
			
				}
				return false;
			}
		});
		xinhaoqinagdu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 showRssi();
			}
		});
		
		ftpceshi.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_ftp);
					
				}
				return false;
			}
		});
		ftpceshi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doFtpTest();
			}
		});

		httpceshi.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_http);
					
				}
				return false;
			}
		});
		httpceshi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doHttpTest();
			}
		});
	 
		peap.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_peap);
				 	
				}
				return false;
			}
		});
		peap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 doPeap();
			}
		});
		 
		ping.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_ping);
				}
				return false;
			}
		});
		ping.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doPing();
			}
		});
		web.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.jiemian_web);
				 	
				}
				return false;
			}
		});
		web.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 doWebAuth();
			}
		});
		manyouceshi.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction()==KeyEvent.ACTION_DOWN){
				relation.setBackgroundResource(R.drawable.jiemian_roam);
			
			}
				return false;
			}
		});
		manyouceshi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 doRoamTest();
			}
		});
		
		geliceshi.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction()==KeyEvent.ACTION_DOWN){
					relation.setBackgroundResource(R.drawable.jiemian_seg);
					
				}
				return false;
			}
		});
		geliceshi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doSegregateTest();
			}
		});
		apguanlian.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction()==KeyEvent.ACTION_DOWN){
					relation.setBackgroundResource(R.drawable.jiemian_ap);
					 
				}
				return false;
			}
		});
		apguanlian.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 doApRelevTest();
			}
		});
		oneKey.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				GlobalApp.isWlanFirst = true;
				GlobalApp.TDorGSM_test = "";
				/*if(!isAccountSaved()) {
					return;
				}*/
 
				if (getFTPConfig(ctx) != null) {
					ctx.setContentView(new MoveOrOther(ctx));
					//scanWifi();
				} else {
					showMessage(ctx, "获取FTP账号失败，请检查FTP配置是否正确.");
				}
			}
		});

		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageWhat.WIFI_RECEIVE_LIST:
					dismissMessageDialog();
					showOneKeyWifiListDialog(ctx, wlanManager, handler);
					wlanManager.stopScan();
					break;

				default:

				}
			}
		};
		wlanManager = new WlanManager(ctx);
		wlanManager.setHandler(handler);

		/*
		 * meunGV2.setOnItemClickListener(new OnItemClickListener() { public
		 * void onItemClick(AdapterView<?> arg0, View v, int index, long id) {
		 * switch (index) { case 0: doApRelevTest(); break; case 1:
		 * doSegregateTest(); break; case 2: doRoamTest(); break; } } });
		 * meunGV3.setOnItemClickListener(new OnItemClickListener() { public
		 * void onItemClick(AdapterView<?> arg0, View v, int index, long id) {
		 * switch (index) { case 0: doWifiSNRTest(); break; case 1: showRssi();
		 * break; }
		 * 
		 * } }); meunGV.setOnItemClickListener(new OnItemClickListener() {
		 * public void onItemClick(AdapterView<?> arg0, View v, int index, long
		 * id) { switch (index) { case 0: doWebAuth();
		 * 
		 * break; case 1: doPing();
		 * 
		 * break; case 2: break; case 3: doPeap(); break; case 4: doHttpTest();
		 * break; case 5: doFtpTest(); break; } } }); }
		 */
	}

	protected void scanWifi() {
		showMessageDialog(ctx, "正在扫描附近Wifi热点...");
		new Thread() {
			public void run() {
				wlanManager.startScan();
			}
		}.start();
	}

	private void doPeap() {
		PeapWidget peap = new PeapWidget(ctx);
		ctx.setContentView(peap);
		peap.scanWifi();
	}

	private void doRoamTest() {
		/*ctx.overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);*/
		RoamTestWidget rw = new RoamTestWidget(ctx);
		ctx.setContentView(rw);
	}

//	private void doWifiSNRTest() {
//		WifiSnrTestWidget ww = new WifiSnrTestWidget(ctx);
//		ctx.setContentView(ww);
//	}
	private void doLfileFTPDownload() {
		FtpDownloadWidget fw = new FtpDownloadWidget(ctx);
		ctx.setContentView(fw);
		fw.statFtpTest();
	}

	private void doSegregateTest() {
		SegregateTestWidget sw = new SegregateTestWidget(ctx);
		ctx.setContentView(sw);
	}

	private void doApRelevTest() {
		ApRelevanceTestWidget aw = new ApRelevanceTestWidget(ctx);
		ctx.setContentView(aw);
		aw.scanWifi();
	}

	private void doHttpTest() {
		HttpTestWidget httpTest = new HttpTestWidget(ctx);
		ctx.setContentView(httpTest);
		httpTest.startHttpTest();
	}

	private void doFtpTest() {
		FtpTestWidget ftpTest = new FtpTestWidget(ctx);
		ctx.setContentView(ftpTest);
		ftpTest.statFtpTest();
	}

	private void doPing() {
		PingWidget ping = new PingWidget(ctx);
		ctx.setContentView(ping);
	}

	private void showRssi() {
		RssiWidget rw = new RssiWidget(ctx);
		ctx.setContentView(rw);
	}

	private void doWebAuth() {
		WebauthTestWidget w = new WebauthTestWidget(ctx);
		ctx.setContentView(w);
		// w.start();
	}
}
